package com.kalix.mooc.reply.biz;

import com.kalix.framework.core.api.persistence.JsonData;
import com.kalix.framework.core.api.persistence.JsonStatus;
import com.kalix.framework.core.impl.biz.ShiroGenericBizServiceImpl;
import com.kalix.mooc.reply.api.biz.IDiscussBeanService;
import com.kalix.mooc.reply.api.biz.IReplyBeanService;
import com.kalix.mooc.reply.api.dao.IDiscussBeanDao;
import com.kalix.mooc.reply.api.dto.DiscussDTO;
import com.kalix.mooc.reply.api.dto.DiscussJsonBean;
import com.kalix.mooc.reply.entities.DiscussBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能描述:
 *
 * @Description: 讨论实现类
 * @Author: yyp
 * @CreateDate: 2019/5/20$ 10:34$
 * @updateDate: 2019/7/04$ 14:23$
 * @Version:
 */
public class DiscussBeanServiceImpl extends ShiroGenericBizServiceImpl<IDiscussBeanDao, DiscussBean> implements IDiscussBeanService {
    private IReplyBeanService iReplyBeanService;

    @Override
    /**
     * 增加讨论 2019/05/21
     * */
    public JsonStatus addDiscuss(DiscussBean discuss) {
        DiscussBean discussBean = new DiscussBean();
        discussBean.setUserId(discuss.getUserId());
        discussBean.setUserName(discuss.getUserName());
        Date t = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       //  System.out.println(df.format(t));
        discussBean.setDateTime(df.format(t));
        discussBean.setDiscussTitle(discuss.getDiscussTitle());
        discussBean.setDiscussEnclosure(discuss.getDiscussEnclosure());
        discussBean.setThumbsupCounts(0l);
        discussBean.setCid(discuss.getCid());
        discussBean.setCname(discuss.getCname());
        DiscussBean discussBean1 = dao.save(discussBean);
        JsonStatus jsonS = new JsonStatus();
        if (discussBean1 != null) {
            jsonS.setSuccess(true);
            jsonS.setMsg("保存成功！");
        } else {
            jsonS.setFailure(true);
            jsonS.setMsg("保存失败！");
        }
        return jsonS;
    }

    /**
     * 回复管理左侧菜单 2019/05/22
     */
    @Override
    public JsonData getReplyForTree() {
        List<DiscussJsonBean> list = new ArrayList<>();
        List<DiscussBean> modelList = dao.find("select f from DiscussBean f order by f.id desc");
        if (modelList.size() > 0) {
            for (DiscussBean discussBean : modelList) {
                DiscussJsonBean discussJsonBean = new DiscussJsonBean();
                discussJsonBean.setValue(discussBean.getId());
                discussJsonBean.setLabel(discussBean.getDiscussTitle());
                discussJsonBean.setReplyEnclosure(discussBean.getDiscussEnclosure());
                list.add(discussJsonBean);
            }
        }
        JsonData jsonData = new JsonData();
        jsonData.setData(list);
        jsonData.setTotalCount((long) list.size());
        return jsonData;
    }


    @Override
    public void deleteAllById(long id) {
        dao.remove(id);
        iReplyBeanService.deleteAllByPostid(id);

    }


    public void setiReplyBeanService(IReplyBeanService iReplyBeanService) {
        this.iReplyBeanService = iReplyBeanService;
    }

    public JsonData getAllDiscuss(String a) {
        String sql = "select d.id,d.userid,d.username,d.discusstitle,d.discussenclosure,d.thumbsupCounts,d.cid,d.cname,d.updateDate,d.datetime from mooc_discuss d, mooc_course c where c.id = d.cid";
        if (!a.isEmpty() && a!=null) {
            sql += " and d.cid=" + a;
        }
        List<DiscussDTO> list = dao.findByNativeSql(sql, DiscussDTO.class);
        JsonData jsonData = new JsonData();
        jsonData.setData(list);
        jsonData.setTotalCount((long) list.size());
        return jsonData;
    }
}
